import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
export function toParams(query) {
    var q = query.replace(/^\??\//, "");
    return q.split("&").reduce(function(values, param) {
        var _param_split = _sliced_to_array(param.split("="), 2), key = _param_split[0], value = _param_split[1];
        values[key] = value;
        return values;
    }, {});
}
export function toQuery(params) {
    var delimiter = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "&";
    var keys = Object.keys(params);
    return keys.reduce(function(str, key, index) {
        if (typeof params[key] === "undefined" || params[key] === null) return;
        var query = "".concat(str).concat(key, "=").concat(params[key]);
        if (index < keys.length - 1) {
            query += delimiter;
        }
        return query;
    }, "");
}
var OAuth2Window = /*#__PURE__*/ function() {
    "use strict";
    function OAuth2Window(id, url) {
        var option = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
        _class_call_check(this, OAuth2Window);
        this.id = id;
        this.url = url;
        this.options = option;
    }
    _create_class(OAuth2Window, [
        {
            key: "open",
            value: function open() {
                var _this = this, url = _this.url, id = _this.id, options = _this.options;
                this.window = window.open(url, id, toQuery(options || {}));
            }
        },
        {
            key: "close",
            value: function close() {
                var _this_window;
                this.cancel();
                (_this_window = this.window) === null || _this_window === void 0 ? void 0 : _this_window.close();
            }
        },
        {
            key: "cancel",
            value: function cancel() {
                if (this._iid) {
                    window.clearInterval(this._iid);
                    this._iid = null;
                }
            }
        },
        {
            key: "then",
            value: function then() {
                for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
                    args[_key] = arguments[_key];
                }
                var _this_promise;
                var _this_promise1;
                return (_this_promise1 = this.promise) === null || _this_promise1 === void 0 ? void 0 : (_this_promise = _this_promise1).then.apply(_this_promise, _to_consumable_array(args));
            }
        },
        {
            key: "catch",
            value: function _catch() {
                for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
                    args[_key] = arguments[_key];
                }
                var _this_promise;
                var _this_promise1;
                return (_this_promise1 = this.promise) === null || _this_promise1 === void 0 ? void 0 : (_this_promise = _this_promise1).then.apply(_this_promise, _to_consumable_array(args));
            }
        },
        {
            key: "poll",
            value: function poll() {
                var _this = this;
                this.promise = new Promise(function(resolve, reject) {
                    _this._iid = window.setInterval(function() {
                        try {
                            var popup = _this.window;
                            if (!popup || popup.closed !== false) {
                                _this.close();
                                reject(new Error("The popup was closed"));
                                return;
                            }
                            if (popup.location.href === _this.url || popup.location.pathname === "blank") {
                                return;
                            }
                            var params = toParams(popup.location.search.replace(/^\?/, ""));
                            resolve(params);
                            _this.close();
                        } catch (error) {
                            /*
           * Ignore DOMException: Blocked a frame with origin from accessing a
           * cross-origin frame.
           */ console.log("abcd");
                        }
                    }, 500);
                });
            }
        }
    ], [
        {
            key: "open",
            value: function open(id, url, options) {
                var popup = new this(id, url, options);
                popup.open();
                popup.poll();
                return popup;
            }
        }
    ]);
    return OAuth2Window;
}();
export { OAuth2Window as default };

 //# sourceMappingURL=oauth.js.map